package cn.fl.insurance.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsCompChnPolQueryCondition;
import cn.fl.insurance.constant.InsuranceConstant;
import cn.fl.insurance.dto.InsCompChnPolDTO;
import cn.fl.insurance.service.IInsCompChnPolApiService;
import cn.fl.insurance.service.IInsCompChnPolService;
import cn.fl.insurance.service.IInsPlcBscInfoService;
import cn.fl.insurance.transform.InsCompChnPolVOTransform;
import cn.fl.insurance.vo.InsCompChnPolVO;

/**
 * @Description: 渠道政策 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsCompChnPolServiceImpl implements IInsCompChnPolService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsCompChnPolServiceImpl.class);

    /**
     * 自定义 API 暴露服务
     */
    @Autowired
    private IInsCompChnPolApiService insCompChnPolApiService;

    /**
     * 数据字典 服务层BEAN
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private IInsPlcBscInfoService insPlcBscInfoService;

    @Override
    public InsCompChnPolVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        InsCompChnPolDTO dto = this.insCompChnPolApiService.selectInsCompChnPolById(id);
        return InsCompChnPolVOTransform.toVO(dto);
    }

    @Override
    public Long add(InsCompChnPolVO entity) throws BaseException {
        if (null == entity) return null;
        return this.insCompChnPolApiService.addInsCompChnPol(InsCompChnPolVOTransform.toDTO(entity));
    }

    /**
     * 批量新增InsCompChnPol
     *
     * @param entityList
     * @return
     * @throws BaseException
     */
    @Override
    public Boolean batchAddInsCompChnPol(List<InsCompChnPolVO> entityList) throws BaseException {
        if (null == entityList) return false;
        return this.insCompChnPolApiService.batchAddInsCompChnPol(InsCompChnPolVOTransform.toDTOList(entityList));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.insCompChnPolApiService.removeInsCompChnPolByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(InsCompChnPolVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insCompChnPolApiService.modifyInsCompChnPol(InsCompChnPolVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page=this.insCompChnPolApiService.searchInsCompChnPolListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<InsCompChnPolDTO> dtoList=(List<InsCompChnPolDTO>)page.getResultList();
            page.setResultList(InsCompChnPolVOTransform.toVOList(dtoList));
        }
        return page;
    }

    @Override
    public List<InsCompChnPolDTO> searchList(InsCompChnPolQueryCondition condition) throws BaseException {
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        return this.insCompChnPolApiService.searchInsCompChnPolList(condition);
    }

    /**
     * 查询保险险种数据字典
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchInsTypCdList() throws BaseException {
        //return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        return this.insPlcBscInfoService.searchInsTypCdList();
    }

    /**
     * 查询保险产品类型数据字典
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchPrdTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_PRD_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 查询返利性质
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchRebNatCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(InsuranceConstant.CODE_INS_COMP_REB_NAT_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 保险支公司一次性渠道申请
     *
     * @param entityList
     * @return
     * @throws BaseException
     */
    @Override
    public DataResultVO addChnPolApply(List<InsCompChnPolVO> entityList) throws BaseException {
        if(CollectionUtils.isEmpty(entityList)){
            return new DataResultVO(BaseConstant.IS_YESNO_NO,"流程发起失败,渠道政策为空");
        }
        InsCompChnPolQueryCondition condition=new InsCompChnPolQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());
        condition.setUsrNm(CurrentThreadContext.getCurrentUserName());
        DataResultVO resultVO= null;
        //发起一次性渠道审核流程
        DataResultDTO resultDTO=this.insCompChnPolApiService.addChnPolApply(InsCompChnPolVOTransform.toDTOList(entityList),condition);
        if(resultDTO!=null){
            resultVO=new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo());
        }else{
            resultVO=new DataResultVO(BaseConstant.IS_YESNO_NO,"流程发起失败");
        }
        return resultVO;
    }
}
